package com.vtb.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vtb.entity.DataCache;
import com.vtb.entity.Var;

@SuppressWarnings("serial")
public class CloseServlet extends OpenAndClose {
	private static final Log log = LogFactory.getLog(CloseServlet.class);
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		super.doPost(request, response);
		if ("q2r".equals(a2b)) {
		    if (rrOauth.getR2q().equals(Var.CLOSE)) {  
                if (!closeTb(qqOauth, rrOauth)) {
                    response.sendRedirect("error?err_code=1001");
                    return;
                }
            }else {   
                qqOauth.setQ2r(Var.CLOSE);
                try {
                    qqDao.update(qqOauth);
                    DataCache.qqmap.put(qqOauth.getUni(), qqOauth);
                    session.setAttribute("q2r", "open");
                } catch (Exception e) {
                    qqOauth.setQ2r(Var.OPEN);   
                    log.error("关闭腾讯微博到人人同步错误：", e);
                    response.sendRedirect("error?err_code=1001");
                    return;
                }
            }
        }
		if ("r2q".equals(a2b)) {
		    if (qqOauth.getQ2r().equals(Var.CLOSE)) {  
                if (!closeTb(qqOauth, rrOauth)) {
                    response.sendRedirect("error?err_code=1001");
                    return;
                }
            }else {   
                rrOauth.setR2q(Var.CLOSE);
                try {
                    rrDao.update(rrOauth);
                    DataCache.rrmap.put(rrOauth.getUni(), rrOauth);
                    session.setAttribute("r2q", "open");
                } catch (Exception e) {
                    rrOauth.setR2q(Var.OPEN);  
                    log.error("关闭人人到腾讯微博同步错误：", e);
                    response.sendRedirect("error?err_code=1001");
                    return;
                }
            }
        }
		if ("r2s".equals(a2b)) {
            if (ssOauth.getS2r().equals(Var.CLOSE)) {  
                if (!closeTb(rrOauth, ssOauth)) {
                    response.sendRedirect("error?err_code=1001");
                    return;
                }
            }else {   
                rrOauth.setR2s(Var.CLOSE);
                try {
                    rrDao.update(rrOauth);
                    DataCache.rrmap.put(rrOauth.getUni(), rrOauth);
                    session.setAttribute("r2s", "open");
                } catch (Exception e) {
                    rrOauth.setR2s(Var.OPEN);  
                    log.error("关闭人人到新浪微博同步错误：", e);
                    response.sendRedirect("error?err_code=1001");
                    return;
                }
            }
        }
		if ("s2r".equals(a2b)) {
		    if (rrOauth.getR2s().equals(Var.CLOSE)) {  
                if (!closeTb(rrOauth, ssOauth)) {
                    response.sendRedirect("error?err_code=1001");
                    return;
                }
            }else {   
                ssOauth.setS2r(Var.CLOSE);
                try {
                    ssDao.update(ssOauth);
                    DataCache.ssmap.put(ssOauth.getUni(), ssOauth);
                    session.setAttribute("s2r", "open");
                } catch (Exception e) {
                    ssOauth.setS2r(Var.OPEN);  
                    log.error("关闭新浪微博到人人同步错误：", e);
                    response.sendRedirect("error?err_code=1001");
                    return;
                }
            }
        }
		if ("q2s".equals(a2b)) {
            if (ssOauth.getS2q().equals(Var.CLOSE)) {  
                if (!closeTb(qqOauth, ssOauth)) {
                    response.sendRedirect("error?err_code=1001");
                    return;
                }
            }else {   
                qqOauth.setQ2s(Var.CLOSE);
                try {
                    qqDao.update(qqOauth);
                    DataCache.qqmap.put(qqOauth.getUni(), qqOauth);
                    session.setAttribute("q2s", "open");
                } catch (Exception e) {
                    qqOauth.setQ2s(Var.OPEN);  
                    log.error("关闭腾讯微博到新浪微博同步错误：", e);
                    response.sendRedirect("error?err_code=1001");
                    return;
                }
            }
        }
        if ("s2q".equals(a2b)) {
            if (qqOauth.getQ2s().equals(Var.CLOSE)) {  
                if (!closeTb(qqOauth, ssOauth)) {
                    response.sendRedirect("error?err_code=1001");
                    return;
                }
            }else {   
                ssOauth.setS2q(Var.CLOSE);
                try {
                    ssDao.update(ssOauth);
                    DataCache.ssmap.put(ssOauth.getUni(), ssOauth);
                    session.setAttribute("s2q", "open");
                } catch (Exception e) {
                    ssOauth.setS2q(Var.OPEN);  
                    log.error("关闭新浪微博到腾讯微博同步错误：", e);
                    response.sendRedirect("error?err_code=1001");
                    return;
                }
            }
        }
		lastMap.put("qq", qqOauth);
		lastMap.put("rr", rrOauth);
		lastMap.put("ss", ssOauth);
		session.setAttribute("lastMap", lastMap);
		response.sendRedirect("");
	}
}
